package com.probox.common.entity.service.order.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.order.api.domain.OrderShipAutoVo;
import com.probox.common.entity.domain.order.OrderShipAuto;
import com.probox.common.entity.mapper.order.OrderShipAutoMapper;
import com.probox.common.entity.service.order.IOrderShipAutoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 自动收货时间Service业务层处理
 *
 * @author hds
 * @date 2021-05-28
 */
@Service
@Transactional
public class OrderShipAutoServiceImpl extends ServiceImpl<OrderShipAutoMapper, OrderShipAuto> implements IOrderShipAutoService {
    @Autowired
    private OrderShipAutoMapper orderShipAutoMapper;


    /**
     * 查询自动收货时间
     *
     * @param autoUid 自动收货时间ID
     * @return 自动收货时间
     */
    @Override
    public OrderShipAuto selectOrderShipAutoById(Integer autoUid) {
        return this.getById(autoUid);
    }

    /**
     * 查询自动收货时间列表
     *
     * @param orderShipAuto 自动收货时间
     * @return 自动收货时间
     */
    @Override
    public List<OrderShipAuto> queryList(OrderShipAuto orderShipAuto) {
        QueryWrapper<OrderShipAuto> queryWrapper = Wrappers.query();

        if (StringUtils.isNotEmpty(String.valueOf(orderShipAuto.getAutoDate())) && orderShipAuto.getAutoDate() != null) {
            queryWrapper.eq("AUTO_DATE", orderShipAuto.getAutoDate());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShipAuto.getDelayedDate())) && orderShipAuto.getDelayedDate() != null) {
            queryWrapper.eq("DELAYED_DATE", orderShipAuto.getDelayedDate());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderShipAuto.getUpdateUser())) && orderShipAuto.getUpdateUser() != null) {
            queryWrapper.eq("UPDATE_USER", orderShipAuto.getUpdateUser());
        }
        Long platFormUid = SecurityUtils.getPlatformId();
        if (platFormUid!=null && platFormUid>0)
            queryWrapper.eq("PLATFORM_UID",platFormUid);
        else
            throw new BaseException("账号异常");
        return orderShipAutoMapper.selectList(queryWrapper);
    }

    /**
     * 新增自动收货时间
     *
     * @param orderShipAuto 自动收货时间
     * @return 结果
     */
    @Override
    public boolean insertOrderShipAuto(OrderShipAuto orderShipAuto) {
        Long accountUid = SecurityUtils.getUserId();
        Long platFormUid = SecurityUtils.getPlatformId();
        String accountName = SecurityUtils.getUsername();
        orderShipAuto.setPlatformUid(platFormUid);
        orderShipAuto.setUpdateUser(accountUid);
        orderShipAuto.setUpdateTime(DateUtil.date());
        orderShipAuto.setUpdateName(accountName);
        return this.save(orderShipAuto);

    }

    /**
     * 修改自动收货时间
     *
     * @param orderShipAuto 自动收货时间
     * @return 结果
     */
    @Override
    public boolean updateOrderShipAuto(OrderShipAuto orderShipAuto) {
        Long accountUid = SecurityUtils.getUserId();
        Long platFormUid = SecurityUtils.getPlatformId();
        String accountName = SecurityUtils.getUsername();
        orderShipAuto.setPlatformUid(platFormUid);
        orderShipAuto.setUpdateUser(accountUid);
        orderShipAuto.setUpdateTime(DateUtil.date());
        orderShipAuto.setUpdateName(accountName);
        return this.updateById(orderShipAuto);
    }

    /**
     * 批量删除自动收货时间
     *
     * @param autoUids 需要删除的自动收货时间ID
     * @return 结果
     */
    @Override
    public boolean deleteOrderShipAutoByIds(Integer[] autoUids) {

        return this.removeByIds(Arrays.asList(autoUids));
    }

    /**
     * 根据平台查询自动收货配置
     * @param platformUid
     * @return
     */
    @Override
    public OrderShipAuto selectByPlatformUid(Long platformUid) {
        QueryWrapper<OrderShipAuto> queryWrapper = Wrappers.query();
        queryWrapper.eq("PLATFORM_UID",platformUid);
        List<OrderShipAuto> shipAutoList = orderShipAutoMapper.selectList(queryWrapper);
        if(shipAutoList!=null && shipAutoList.size()>0)
            return shipAutoList.get(0);
        return null;
    }

    /**
     *
     * @param vo
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean addShipAuto(OrderShipAutoVo vo) {
        OrderShipAuto auto = new OrderShipAuto();
        BeanUtils.copyProperties(vo,auto);
        auto.setUpdateTime(new Date());
        return this.saveOrUpdate(auto);
    }

    @Override
    public OrderShipAutoVo getShipAuto(Long platformUid) {
        OrderShipAutoVo vo = null;
        QueryWrapper<OrderShipAuto> queryWrapper = Wrappers.query();
        queryWrapper.eq("PLATFORM_UID",platformUid);
        List<OrderShipAuto> shipAutoList = orderShipAutoMapper.selectList(queryWrapper);
        if(shipAutoList!=null && shipAutoList.size()>0){
            OrderShipAuto auto = shipAutoList.get(0);
            vo = new OrderShipAutoVo();
            BeanUtils.copyProperties(auto,vo);
        }
        return vo;
    }


}
